package com.jichangxiu.framework.aspect;

import cn.hutool.core.util.ObjectUtil;
import com.jichangxiu.common.annotation.Repeat;
import com.jichangxiu.common.context.JcxContext;
import com.jichangxiu.common.utils.StrUtils;
import com.jichangxiu.framework.utils.RepeatUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
public class RepeatAspect {

    @Pointcut("@annotation(repeat)")
    public void pointcutRepeatAround(Repeat repeat) {
    }

    @Around(value = "pointcutRepeatAround(repeat)", argNames = "proceedingJoinPoint , repeat")
    public Object repeatAround(ProceedingJoinPoint proceedingJoinPoint, Repeat repeat) throws Throwable {
        if (ObjectUtil.isNotEmpty(repeat) && StrUtils.isNotEmpty(JcxContext.getJti()) && repeat.enabled())
            return RepeatUtils.setRepeatRequest(proceedingJoinPoint, repeat.second());
        else
            return proceedingJoinPoint.proceed();
    }

}
